package toOffer;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * <h3>leetcode</h3>
 *
 * @author BigFaceCat
 * @date 2024/3/25 15:14
 * @description <p>无重复字符的最长子串</p>
 */
public class No3 {
    public static void main(String[] args) {
        String s = "";
        System.out.println(lengthOfLongestSubstring(s));
    }

    /**
     * 思路：
     * 滑动窗口，两个指针，如果右指针对应的数加进来不满足条件了，那么就让左指针右移、减少窗口内的数据，直到满足条件
     * @param s
     * @return
     */
    public static int lengthOfLongestSubstring(String s) {
        int left = 0;
        int right = 0;
        int count = 0;
        Map<Character, Integer> map = new HashMap<>();

        while (right < s.length()){
            char c = s.charAt(right);
            while (map.containsKey(c)){
                left = Math.max(left, map.get(c) + 1);
            }
            map.put(c, right);
            count = Math.max(count, right - left + 1);
            right++;
        }
        return count;
    }
}
